<template>
  <div>
    <basic-modal :modalOpt="modal" :footer="null" @closeModal="closeModal">
      <template>
        <basic-form
          :formInfos="s_FormInfo"
          :formDatas="s_formDatas"
          :rules="s_rules"
          @onSubmit="onSubmit"
          @onChangeType="onChangeType"
          @closeModal="closeModal"
        ></basic-form>
      </template>
    </basic-modal>
  </div>
</template>

<script>
import basicModal from '@/views/app/biz/common/basicModal'
import basicForm from '../common/basicForm'
import { getLicenseByPid } from '@/views/app/biz/regisLicense/regisLicense'
import { getInfoByPid, saveInfo } from './manufacturer'

const s_FormInfo = [
  // { label: '所属单位', name: 'orgPid', type: 'input', placeholder: '请输入所属单位' },
  // { label: '生产企业Pid', name: 'manufacturerOrgPid', type: 'input', placeholder: '请输入生产企业Pid' },
  { label: '生产企业社会编码', name: 'manufacturerCode', type: 'input', placeholder: '请输入生产企业社会编码' },
  { label: '生产企业中文名称', name: 'manufacturerZh', type: 'input', placeholder: '请输入生产企业中文名称' },
  { label: '生产企业英文名称', name: 'manufacturerEn', type: 'input', placeholder: '请输入生产企业英文名称' },
  { label: '生产企业城市', name: 'city', type: 'input', placeholder: '请输入生产企业城市' },
  { label: '生产企业地点', name: 'addr', type: 'input', placeholder: '请输入生产企业地点' },
  { label: '是否外资', name: 'foreign', type: 'radio2',options:[{id:"是",value:'是'},{id:"否",value:'否'}], placeholder: '请选择是否外资' },
  // { label: '有效期', name: 'licenceValidDate', type: 'input', placeholder: '请输入有效期' },
  // { label: '主证照信息', name: 'licenceMaster', type: 'input', placeholder: '请输入主证照信息' },
  // { label: '其他证照信息', name: 'licences', type: 'input', placeholder: '请输入其他证照信息' },
  { label: ['保存', '重置'], type: 'botton' }
]

const s_formDatas = {
  sysSeq: 0,
  pid: '',
  manufacturerOrgPid: null,
  manufacturerCode: null,
  manufacturerZh: null,
  manufacturerEn: null,
  city: null,
  addr: null,
  foreign: "1",
  licenceValidDate: null,
  licenceMaster: null,
  licences: null,
  state: null
}
const s_rules = {
  manufacturerCode: [{ required: true, message: '生产企业社会编码必填', trigger: 'blur' }],
  manufacturerZh: [{ required: true, message: '生产企业中文名称必填', trigger: 'blur' }]
}
export default {
  components: {
    basicModal,
    basicForm
  },
  data() {
    return {
      loading: false,
      modal: {
        modalVisible: false,
        modalTitle: '新增商品',
        modalWidth: '1000px'
      },
      s_FormInfo: s_FormInfo,
      s_formDatas: s_formDatas,
      s_rules: s_rules
    }
  },
  computed: {},
  mounted() {},
  methods: {
    //自动生成FormData
    autoFormData() {
      let str = ''
      for (const item of s_FormInfo) {
        str += item.name + ': null,'
      }
      console.log(str)
    },
    // 新增按钮 点击了新增
    addModal() {
      this.autoFormData()
      console.log('点击了新增')
      this.s_formDatas = {
        sysSeq: 0,
        pid: '',
        code: null,
        productNameZh: null,
        productNameEn: null,
        commonName: null,
        class: ['f11', 's22', 't22'],
        first: 'f11',
        second: 's22',
        third: null,
        spec: null,
        model: null,
        medicareCode: null,
        disc: null,
        brand: null,
        mahCode: null,
        mahZh: null,
        mahEn: null,
        manufacturerCode: null,
        manufacturerZh: null,
        manufacturerEn: null,
        origin: null,
        category: null,
        measurement: null,
        sdc: null,
        approvalType: null,
        approvalNum: null,
        approvalDate: null,
        validDate: null,
        state: null,
        settlement: null,
        acquisitionCode: null
      }
      this.modal.modalVisible = true
      this.modal.modalTitle = '新增商品'
    },
    // 编辑按钮
    editModal(val) {
      console.log('点击了编辑')
      this.getSBypid(val)
      this.modal.modalVisible = true
      this.modal.modalTitle = '修改商品'
    },
    // 关闭模态详细框
    closeModal() {
      this.modal.modalVisible = false
      this.$emit('close')
    },
    // 保存成功关闭模态详细框
    closeModal2() {
      this.modal.modalVisible = false
      this.$emit('ok')
    },
    // 模态详细框提交按钮
    onSubmit(val) {
      this.saveObjInfo(val)
    },
    // 保存详细信息
    async saveObjInfo(val) {
      this.loading = true
      // this.$message.loading({ content: '提交中'， })
      let res = await saveInfo(val)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '提交成功', duration: 2 })
        this.closeModal2()
      } else {
        this.$message.error({ content: '提交失败', duration: 2 })
      }
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        if (res.data) {
          this.s_formDatas = res.data
        } else {
          this.addModal()
        }
      }
      console.log(res)
    },
    // 表单编号
    onChangeType(name, val) {
      console.log('formName', name)
      console.log('val', val)
      // switch (name) {
      //   case 'formName':
      //     // console.log('aaaaaaaaaaaaaa',name)
      //     // const zhucez = this.getZhuCezxx(val)
      //     break
      // }
    },
    // 获取注册证详细信息
    async getZhuCezxx(pid) {
      const res = await getLicenseByPid(pid)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.s_formDatas = Object.assign(this.s_formDatas, { zhuCezhPid: res.data.pid })
        this.s_formDatas = Object.assign(this.s_formDatas, { zhuCezh: res.data.zhengZhaohm })
        this.s_formDatas = Object.assign(this.s_formDatas, { shengChanqymc: res.data.shengChancsmc })
        this.s_formDatas = Object.assign(this.s_formDatas, { pinMing: res.data.chanPinmc })
      }
    },
    //
    restData(val) {
      this.getSBypid(this.s_formDatas.pid)
    }
  }
}
</script>

<style></style>
